<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    body {
      height: 200vh;
      background-image: linear-gradient(white, black);
    }
  </style>
</head>

<body>
  <script>
    function onScroll() {
      // 方式一：通过 window 的 scrollX/scrollY
      console.log('window.scrollX', window.scrollX)
      console.log('window.scrollY', window.scrollY)

      // 方式二：通过 HTML 元素的 scrollTop/scrollLeft
      console.log('document.documentElement.scrollTop', document.documentElement.scrollTop)
      console.log('document.documentElement.scrollLeft', document.documentElement.scrollLeft)
    }

    window.addEventListener('scroll', onScroll)
  </script>
</body>

</html>